{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "419f59cf-42b9-4180-9488-2a1b9d985d0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95c9b496-fc79-4abd-9e83-39e49d2a7eaf",
   "metadata": {},
   "source": [
    "# 数据类型相关"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e45d23d-126c-4ef6-aea2-699723ff29ce",
   "metadata": {
    "tags": [],
    "toc-hr-collapsed": true
   },
   "source": [
    "## 数据类型相关\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "can_cast(from_, to[, casting])|如果根据强制转换规则可以在数据类型之间进行强制转换，则返回True。\n",
    "promote_types(type1, type2)|返回Type1和Type2都可以安全强制转换为的最小大小和最小标量种类的数据类型。\n",
    "min_scalar_type(a)|对于标量a，返回具有最小大小和可以保存其值的最小标量种类的数据类型。\n",
    "result_type(*arrays_and_dtypes)|返回将NumPy类型提升规则应用于参数而得到的类型。\n",
    "common_type(*arrays)|返回输入数组通用的标量类型。\n",
    "obj2sctype(rep[, default])|返回对象的Python类型的标量dtype或NumPy等效值。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8a8c8ac-4a75-4278-8f4b-b82d4a5a6239",
   "metadata": {},
   "source": [
    "### numpy.can_cast(from_, to, casting='safe')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "37403af1-dad2-44ef-850b-2c377e179cbd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.can_cast(np.int32, np.int64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1cdb73ef-7894-421f-b9e2-d8e99658093c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.can_cast(np.float64, complex)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a841bdcf-b346-49b2-9915-7e2669dc08af",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.can_cast(complex, float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ae989b39-6a0e-42e6-b256-79f4ca28b2b2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.can_cast('i8', 'f8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "90aa6391-c02b-42ee-95a2-4e02a616f3e8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.can_cast('i8', 'f4')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "21141320-6f66-4d16-bce2-5dba7d5844b5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.can_cast('i4', 'S4')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a1bd198-89af-49d3-803d-a9966b2ca58b",
   "metadata": {},
   "source": [
    "### numpy.promote_types(type1, type2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6a15c31e-8650-41eb-9d31-8fda0a374ba4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.promote_types('f4', 'f8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7c28e54b-6233-4b35-98b3-969b78ef6e43",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.promote_types('i8', 'f4')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6cb54c3c-c847-453d-adcf-326a7417633c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('complex128')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.promote_types('>i8', '<c8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5b9211b2-7977-4a65-9a5b-ed8c65f2451a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('S11')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.promote_types('i4', 'S8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e54698ba-10bd-4467-a44c-3511e6bd7bb8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('S6')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = np.promote_types\n",
    "p('S', p('i1', 'u1'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1ecfd1dd-f171-4a36-bcfb-17d53379a132",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('S4')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p(p('S', 'i1'), 'u1')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ef89a41-a5d7-49b1-a177-73bdcda2ca13",
   "metadata": {},
   "source": [
    "### numpy.min_scalar_type(a, /)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b603878c-8f3e-4a5d-8ca3-451609b1ab00",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('uint8')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.min_scalar_type(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7131274b-bf8b-4aa2-851e-6b13a198ef55",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int16')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.min_scalar_type(-260)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "99364838-8141-4814-a136-fbc973f30de0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float16')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.min_scalar_type(3.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "43de574c-19fe-4f26-9a2e-adcb424d0da2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.min_scalar_type(1e50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a1f0029a-4fcb-412c-9b47-306342a2787f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.min_scalar_type(np.arange(4,dtype='f8'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df88552a-4e31-4a7d-a724-67b6bd1edc06",
   "metadata": {},
   "source": [
    "### numpy.result_type(*arrays_and_dtypes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0b659e56-7321-4842-887f-b3c9c9963652",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int8')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.result_type(3, np.arange(7, dtype='i1'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "4a7627c8-f141-4fd4-a71d-5255995d0c16",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('complex128')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.result_type('i4', 'c8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a6a66e37-f997-4c6b-a11d-0e06e9c5a23a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.result_type(3.0, -2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08540437-80ef-4268-98c0-85f8e1287a15",
   "metadata": {},
   "source": [
    "### numpy.common_type(*arrays)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2669fde8-6236-4a15-b3ed-5889056e0ddb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.float32"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.common_type(np.arange(2, dtype=np.float32))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3cfa2016-54fc-4052-8a56-46fcd69881d1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.float64"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.common_type(np.arange(2, dtype=np.float32), np.arange(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f77b9188-777c-499d-8de5-239f84202e8c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.complex128"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.common_type(np.arange(4), np.array([45, 6.j]), np.array([45.0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "438e99e7-d5ec-4d0e-8244-39f5f33de248",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "## 创建数据类型\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "dtype(obj[, align, copy])|创建数据类型对象。\n",
    "format_parser(formats, names, titles[, …])|类将格式、名称、标题说明转换为dtype。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7be4915d-a8d7-46aa-ab81-fd9cc70a4118",
   "metadata": {},
   "source": [
    "### class numpy.dtype(dtype, align=False, copy=False[, metadata])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "8e4a4756-353b-4327-aa62-af9bd654d9b5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int16')"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dtype(np.int16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "ab4ada54-3f4b-44e1-8271-3f085b2ab703",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype([('f1', '<i2')])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dtype([('f1', np.int16)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6b157fa7-9397-4a2f-8a6f-156127002647",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype([('f1', [('f1', '<i2')])])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dtype([('f1', [('f1', np.int16)])])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "1e4dc00a-b656-4b19-9d38-e9f70466a082",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype([('f1', '<u8'), ('f2', '<i4')])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dtype([('f1', np.uint64), ('f2', np.int32)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "98ff380d-c438-40cd-a82e-5ea0650e9415",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype([('a', '<f8'), ('b', 'S10')])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dtype([('a','f8'),('b','S10')])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4bb47800-ec73-4346-8f47-32ad8d5e2c6d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype([('f0', '<i4'), ('f1', '<f8', (2, 3))])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dtype(\"i4, (2,3)f8\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "9aa1bc7d-9ac1-4f50-b9eb-2478a36b14a6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype([('hello', '<i8', (3,)), ('world', 'V10')])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dtype([('hello',(np.int64,3)),('world',np.void,10)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "81a0f6dd-1bf0-4fb9-a6db-ea647b4c4b07",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype((numpy.int16, [('x', 'i1'), ('y', 'i1')]))"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dtype((np.int16, {'x':(np.int8,0), 'y':(np.int8,1)}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "48dbd7aa-f8b3-4bfa-91fd-464120e7834e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype([('gender', 'S1'), ('age', 'u1')])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dtype({'names':['gender','age'], 'formats':['S1',np.uint8]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "bd407d2e-ab37-4ab1-9612-0f81fae155c0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype([('surname', 'S25'), ('age', 'u1')])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dtype({'surname':('S25',0),'age':(np.uint8,25)})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "243e04b0-ed88-4c40-ae77-2e81f4358786",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "## 数据类型信息\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "finfo(dtype)|浮点类型的机器限制。\n",
    "iinfo(type)|整数类型的机器限制。\n",
    "MachAr([float_conv, int_conv, …])|诊断机器参数。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f16d8219-ab7a-40ac-ad7f-c3c1e49911c5",
   "metadata": {},
   "source": [
    "### class numpy.finfo(dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "6ea4d20f-85d4-44c9-b073-f8a5e76be305",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.finfo(np.float64).dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "0303e3cf-076e-429e-9afb-1e356e387f69",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float32')"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.finfo(np.complex64).dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e324aa6-74e9-48da-a813-dbe1f4578f3b",
   "metadata": {},
   "source": [
    "### class numpy.iinfo(type)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "08daf6e4-9757-45e1-9598-0eb8443a39a2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-32768"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ii16 = np.iinfo(np.int16)\n",
    "ii16.min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "1e405b31-0344-4acd-8321-fade2e5c125b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32767"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ii16.max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "774dbde7-f24d-456e-b714-b5070f484ee9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-2147483648"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ii32 = np.iinfo(np.int32)\n",
    "ii32.min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "5935449f-89f5-4f11-bbb7-d7f6415db194",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2147483647"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ii32.max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "99412b52-8005-44f9-b724-bc6c821d09b4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-2147483648"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ii32 = np.iinfo(np.int32(10))\n",
    "ii32.min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "750cd3ae-a4da-4dc1-b82e-743f276d6136",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2147483647"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ii32.max"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "082ae40e-1ef2-4b48-bfd0-7e1ecc9550d3",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "## 数据类型测试\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "issctype(rep)|确定给定对象是否表示标量数据类型。\n",
    "issubdtype(arg1, arg2)|如果第一个参数是类型层次结构中较低/等于的类型码，则返回True。\n",
    "issubsctype(arg1, arg2)|确定第一个参数是否是第二个参数的子类。\n",
    "issubclass_(arg1, arg2)|确定一个类是否是第二个类的子类。\n",
    "find_common_type(array_types, scalar_types)|按照标准强制规则确定常见类型。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6826b3d1-64b0-443c-a6dd-146e00873c86",
   "metadata": {},
   "source": [
    "### numpy.issubdtype(arg1, arg2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "25a1bf9e-be83-43dd-bf00-a84c2053f58d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ints = np.array([1, 2, 3], dtype=np.int32)\n",
    "np.issubdtype(ints.dtype, np.integer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "fb642dd1-248b-40de-bdc2-8570a6519ad4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.issubdtype(ints.dtype, np.floating)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "fd08b430-217e-4d31-8ecb-7463d07f1556",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "floats = np.array([1, 2, 3], dtype=np.float32)\n",
    "np.issubdtype(floats.dtype, np.integer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "daa55e81-bec4-42bc-aa5b-c8c8b35a55b7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.issubdtype(floats.dtype, np.floating)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "b0c4477c-b141-44ca-b41d-8e0bdc75321d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.issubdtype(np.float64, np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "e5c84e5a-be37-4b96-ada6-2ff951fd935f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.issubdtype(np.float32, np.float64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "5e4fd5d6-e4c3-467f-8744-2749f212ccf9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.issubdtype(np.float64, np.floating)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "ba58938e-1cb6-4f6a-91fd-2b1c7588dc22",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.issubdtype(np.float32, np.floating)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "bba1951b-6e2a-4488-a98c-029fa481cae5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.issubdtype('S1', np.bytes_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "2783e007-25f1-4f95-bd20-2c9547b448e3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.issubdtype('i4', np.signedinteger)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c44e1ad-1336-4181-abfc-ba2df9cd7487",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "## 杂项\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "typename(char)|返回给定数据类型代码的说明。\n",
    "sctype2char(sctype)|返回标量dtype的字符串表示形式。\n",
    "mintypecode(typechars[, typeset, default])|返回给定类型可以安全强制转换到的最小大小类型的字符。\n",
    "maximum_sctype(t)|返回与输入类型相同精度最高的标量类型。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01c688c4-a7e4-4b6e-b736-7151c0f77cee",
   "metadata": {},
   "source": [
    "### numpy.typename(char)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "27439e2d-a8d2-4754-adf7-581d27b16502",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "S1  :  character\n",
      "?  :  bool\n",
      "B  :  unsigned char\n",
      "D  :  complex double precision\n",
      "G  :  complex long double precision\n",
      "F  :  complex single precision\n",
      "I  :  unsigned integer\n",
      "H  :  unsigned short\n",
      "L  :  unsigned long integer\n",
      "O  :  object\n",
      "Q  :  unsigned long long integer\n",
      "S  :  string\n",
      "U  :  unicode\n",
      "V  :  void\n",
      "b  :  signed char\n",
      "d  :  double precision\n",
      "g  :  long precision\n",
      "f  :  single precision\n",
      "i  :  integer\n",
      "h  :  short\n",
      "l  :  long integer\n",
      "q  :  long long integer\n"
     ]
    }
   ],
   "source": [
    "typechars = ['S1', '?', 'B', 'D', 'G', 'F', 'I', 'H', 'L', 'O', 'Q',\n",
    "             'S', 'U', 'V', 'b', 'd', 'g', 'f', 'i', 'h', 'l', 'q']\n",
    "for typechar in typechars:\n",
    "    print(typechar, ' : ', np.typename(typechar))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a922fc46-c884-4dfb-a1a6-9aac10e659d3",
   "metadata": {},
   "source": [
    "### numpy.mintypecode(typechars, typeset='GDFgdf', default='d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "fbe54938-0dfa-4f07-b2e2-e8ab7a87477f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'d'"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mintypecode(['d', 'f', 'S'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "8c43184d-0d0e-417a-a234-fb140468d594",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([1.1, 2-3.j])\n",
    "np.mintypecode(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "8d84db6b-e4e6-4fcb-9be8-ceb54df72d73",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'G'"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mintypecode('abceh', default='G')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
